#include "type.h"

typedef struct i2c_device_t
{
    uint8_t address;
} i2c_device_t;


/* MASTER TRANSMITTER MODE */
#define START_TRANSMITTED		0x08
#define REP_START_ISSUED    	0x10
#define ADDRESS_W_ACK_RECEIVED	0x18
#define DATA_W_ACK_RECEIVED	    0x28
#define DATA_W_NO_ACK_RECEIVED	0x30
#define NO_ACK_TRANSMITTER      0x20

/* MASTER RECEIVER MODE */
#define ADDRESS_R_ACK_RECEIVED	0x40
#define DATA_R_ACK_RECEIVED	    0x50
#define DATA_R_NO_ACK_RECEIVED	0x58
#define NO_ACK_RECEIVER         0x48

#define ARBITRATION_LOST        0x38

#define PCI2C0_POWERON (1 << 7)
#define PCI2C2_POWERON (1 << 26)

#define FAST_MODE_PLUS      0

#define I2C_PORT_NUM        3
#define BUFSIZE             64
#define MAX_TIMEOUT         0x00001000


#define I2CMASTER           0x01
#define I2CSLAVE            0x02

#define PCF8594_ADDR        0xA0
#define READ_WRITE          0x01

#define RD_BIT              0x01

#define I2C_IDLE              0
#define I2C_STARTED           1
#define I2C_RESTARTED         2
#define I2C_REPEATED_START    3
#define DATA_ACK              4
#define DATA_NACK             5
#define I2C_BUSY              6
#define I2C_NO_DATA           7
#define I2C_NACK_ON_ADDRESS   8
#define I2C_NACK_ON_DATA      9
#define I2C_ARBITRATION_LOST  10
#define I2C_TIME_OUT          11
#define I2C_OK                12


#define I2CONSET_AA         (1<<2)
#define I2CONSET_SI         (1<<3)
#define I2CONSET_STO        (1<<4)
#define I2CONSET_STA        (1<<5)
#define I2CONSET_I2EN       (1<<6)  /* I2C Control Set Register */

#define I2CONCLR_AAC        (1<<2)  /* I2C Control clear Register */
#define I2CONCLR_SIC        (1<<3)
#define I2CONCLR_STAC       (1<<5)
#define I2CONCLR_I2ENC      (1<<6)

#define I2DAT_I2C           0x00000000  /* I2C Data Reg */
#define I2ADR_I2C           0x00000000  /* I2C Slave Address Reg */
#define I2SCLH_SCLH         0x00000080  /* I2C SCL Duty Cycle High Reg */
#define I2SCLL_SCLL         0x00000080  /* I2C SCL Duty Cycle Low Reg */
#define I2SCLH_HS_SCLH      0x00000008  /* Fast Plus I2C SCL Duty Cycle High Reg */
#define I2SCLL_HS_SCLL      0x00000008  /* Fast Plus I2C SCL Duty Cycle Low Reg */

void i2c2_init(void);
uint8_t i2c_write(i2c_device_t *i2c_device, uint8_t addr, uint8_t *data);
uint8_t i2c_read(i2c_device_t *i2c_device, uint8_t addr, uint8_t *data);
uint8_t i2c_read_burst(i2c_device_t *i2c_device, uint8_t addr, uint8_t *data);
uint32_t i2c2_transfer(void);
void i2c2_master_isr(void);

